<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/bootstrap/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/bootstrap/css/bootstrap-theme.min.css"/>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/Css/style.css"/>
    <link href="<%=request.getContextPath()%>/Css/jquery-ui.min.css"
          type="text/css" rel="stylesheet"/>
     <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery2.sorted.js"></script>
     <script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/ckform.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/common.js"></script>
    <script type="text/javascript"
            src="<%=request.getContextPath()%>/js/jquery-ui.min.js"></script>


    <style type="text/css">
        /*#addMessageDiv{
            padding-top: 100px;
            padding-left: 50px;
        }*/
      /*  span{

            font-size: x-large;

        }*/
        tr{
            padding-top: 20px;
        }
		.requird{
			color:#F00;
			margin-right:2px;
		}
    </style>
</head>
<body>

<form id="msgform" class="form-inline definewidth m20" action="/" method="post">
    <button type="button" class="btn btn-primary" onclick="addJobDefine();">新增</button>
    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#noticeModal">发送公告</button>
</form>
<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th width="10%">序号</th>
        <th width="50%">公告内容</th>
        <th width="10%">公告类型</th>
        <th width="10%">发布时间</th>
        <th width="10%">发布人</th>
        <th width="20%">信息操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${rows}" var="msg" varStatus="status">
        <tr id="${noticeId}">
            <td name="id">${status.index+1}</td>
            <td name="content">
            	<div class="well well-sm">${msg.content}</div>
            </td>
            <td name="noticeType"><c:if test="${msg.noticeType==0}">循环公告</c:if>
                <c:if test="${msg.noticeType==1}">公告栏公告</c:if></td>
            <td name="createTime"><fmt:formatDate value="${msg.createTime }" pattern="yyyy-MM-dd HH:mm:ss"/></td>
            <td name="createUser">${msg.createUser }</td>
            <td>
            	<div class="notice_content" style="display: none;">${msg.content}</div>
                <a href="#" onclick="editAndSend('${msg.noticeType}',this)" class="btn btn-primary">编辑</a>
            </td>
        </tr>
    </c:forEach>
    </tbody>
    <tfoot>
    <tr bgcolor="EEF7FE">
        <td colspan="14" bgcolor="EEF7FE" class="f">
            <div align="right" style="bgcolor:EEF7FE" class="f">
                <jsp:include page="/common/pageUtil.jsp"></jsp:include>
            </div>
        </td>
    </tr>
    </tfoot>
</table>


    <div id="addMessageDiv" style="display: none;" >
        <form action="/" method="post" id="addMessageForm">
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <td><label for="description"><span
                            style="color:#F00; margin-right:2px;">*</span>消息：
                    </label>
                    </td>
                    <td><textarea id="description" name="description" type='text' rows="3" cols="20" >${conten}</textarea></td>
                </tr>

                <tr>
                    <td><label for="frequency"><span
                            style="color:#F00; margin-right:2px;">*</span>显示频率(秒)：
                    </label>
                    </td>
                    <td><input id="frequency" name="frequency" value="${frequency}"/>
                    </td>
                </tr>
                <%--<tr>
                    <td> <button type="button" class="btn btn-primary" onclick="confirmSetting();">确认</button></td>
                    <td> <button type="button" class="btn btn-primary" onclick="stopMsg();">停止</button></td>
                </tr>--%>
            </table>
        </form>
    </div>


	<div class="modal fade" id="noticeModal" tabindex="-1" role="dialog" aria-labelledby="noticeModalLabel">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="myModalLabel">发送公告消息</h4>
	      </div>
	      <div class="modal-body">
	        <form class="form-horizontal">
				  <div class="form-group">
				    <label for="inputEmail3" class="col-sm-2 control-label"><span class="requird">*</span>公告类型:</label>
				    <div class="col-sm-10">
				      <select class="form-control" id="noticeType" placeholder="Email">
				      	<option value=0>循环公告</option>
				      	<option value=1>布告栏公告</option>
				      </select>
				    </div>
				  </div>
				  <div class="form-group">
				    <label for="inputPassword3" class="col-sm-2 control-label">内容:</label>
				    <div class="col-sm-10">
				      <textarea id="content" placeholder="消息内容" class="form-control" rows="3"></textarea>
				    </div>
				  </div>
				</form>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	        <button type="button" class="btn btn-primary" onclick="sendMsg()">发送</button>
	      </div>
	    </div>
	  </div>
	</div>
</body>
<script type="text/javascript">


    function startJob(jobId,obj){

        var parent = $(obj).parent().parent();
        var info = $(parent).find("td[name='jobStatus']").text();
        if (info == '已启动') {
            return;
        }
        $.ajax({
            type: "POST",
            url: "<%=request.getContextPath()%>/job/changeJobStatus.html",
            data: {
                "jobId": jobId,
                "cmd": "start",
            },
            dataType: 'json',
            success: function (data) {

                var retObj = data.retObj;
                if (retObj.flag) {
                    alert("启动成功");
                    $(parent).find("td[name='jobStatus']").text("已启动");
                }
            }
        });
    }

    function pauseJob(jobId,obj){

        var parent = $(obj).parent().parent();
        var info = $(parent).find("td[name='jobStatus']").text();
        if (info == '暂停') {
            return;
        }
        $.ajax({
            type: "POST",
            url: "<%=request.getContextPath()%>/job/changeJobStatus.html",
            data: {
                "jobId": jobId,
                "cmd": "stop",
            },
            dataType: 'json',
            success: function (data) {
                var retObj = data.retObj;
                if (retObj.flag) {
                    alert("暂停成功");
                    $(parent).find("td[name='jobStatus']").text("暂停");
                }
            }
        });

    }


    function deleteJob(jobId,obj){
        if(confirm("确认删除？")){
            window.location.href="<%=request.getContextPath()%>/job/deleteMessageJob.html?jobId="+jobId;
        }
    }



    function addJobDefine(){
        $("#addMessageDiv").dialog({
            title: "消息任务",
            height: 300,
            width: 400,
            modal: true,
            open: function (event, ui) {
                $(".ui-dialog-titlebar-close", $(this).parent()).hide();
            },
            buttons: {
                "确定": function () {
                    if(confirm("确认添加？")){
                        addMessageInfo();
                    }
                },
                "取消": function () {
                    resetAll();
                    $(this).dialog("close");
                }
            }
        });
    }


    function addMessageInfo() {
        var myform = document.getElementById("addMessageForm");
        myform.action = "<%=request.getContextPath()%>/job/addMessageJob.html";
        myform.submit();


    }
	//发送公告               
    function sendMsg(){
    	var noticeType=$("#noticeType").val(),content=$("#content").val();
    	if($("#content").data("old")==content){
    		alert("内容没有任何改变!");
    		return false;
    	}
    	$.post("<%=request.getContextPath()%>/club/sendNoticeMsg.html",{"noticeType":noticeType,"content":content},function(data){
    		console.log(data);
    		$.when(function(){
	    		alert(data["msg"]);
    		}).done(function(){
    			window.document.location.reload();
    		});
    	},"json");
    }
    
    function resetAll() {
        var myform = document.getElementById("addMessageForm");
        myform.reset();
    }

  function confirmSetting(){

      var msg=$("#msg").val();
      var frequency=$("#frequency").val();

      if(msg==null||msg==''){
          alert("公告内容能为空！！");
          return ;
      }

      if(frequency==null||frequency==''){
          alert("发送频率不能为空！！");
          return ;
      }

      var myform = document.getElementById("addMessageForm");
      myform.action = "<%=request.getContextPath()%>/message/addMsg.html";
      myform.submit();
  }
  function stopMsg(){
        var myform = document.getElementById("addMessageForm");
        myform.action = "<%=request.getContextPath()%>/message/stopMsg.html";
        myform.submit();

   }
function editAndSend(type,el){
	var content=$(el).siblings(".notice_content").text();
	//var reg=new RegExp("<br>","g"); //创建正则RegExp对象    
	//content=content.replace(reg,"\n");
	$("#noticeModal").find("#content").val(content);
	$("#noticeModal").find("#content").data("old",content)
	$("#noticeModal").find("#noticeType").children("option[value="+type+"]").prop("selected",true);
	$("#noticeModal").modal('show');
}
</script>

</html>